/*
 * @Descripttion: 
 * @version: 
 * @Author: fei
 * @Date: 2024-11-14 12:21:39
 * @LastEditors: fei
 * @LastEditTime: 2024-11-28 15:14:11
 */
import React from 'react';
import type { FormProps } from 'antd';
import { Button, Checkbox, Form, Input,message } from 'antd';
import { requestType, useLoginSysMutation, useGetMenuListMutation, menu } from '@/store/apiSlice';
import { useNavigate } from 'react-router-dom';
import { setStore } from '@/utils/storage';
// import { createRoute } from '@/utils/utils';
// import router from '@/route/index.tsx';


type FieldType = {
  username?: string;
  password?: string;
  remember?: string;
};

// const onFinish: FormProps<FieldType>['onFinish'] = (values:requestType) => {
//   loginFun(values)
//   console.log('Success:', values);
// };

const onFinishFailed: FormProps<FieldType>['onFinishFailed'] = (errorInfo) => {
  console.log('Failed:', errorInfo);
};

const Login: React.FC = () => {
  const [ loginFun, {isLoading:loginLoading} ] = useLoginSysMutation();

  const [messageApi, contextHolder] = message.useMessage();

  const navigate = useNavigate();
  const onSavePostClicked = async (params:requestType) => {
    try {
      // highlight-next-line
      const {code,data,message} = await loginFun(params).unwrap();
      if(code === 0){
        setStore('tokens', data.tokens);
        setStore('userInfo', data.userInfo);
      }else{
        messageApi.open({
          type: 'error',
          content: message,
        });
      }

      navigate("/");
      // setTitle("");
      // setContent("");
      // setUserId("");
    } catch (err) {
      console.error("Failed to save the post: ", err);
    }
  };
  return (
  <Form
    name="basic"
    labelCol={{ span: 8 }}
    wrapperCol={{ span: 16 }}
    style={{ maxWidth: 600 }}
    initialValues={{ remember: true }}
    onFinish={onSavePostClicked}
    onFinishFailed={onFinishFailed}
    autoComplete="off"
  >
    <Form.Item<FieldType>
      label="Username"
      name="username"
      rules={[{ required: true, message: 'Please input your username!' }]}
    >
      <Input />
    </Form.Item>

    <Form.Item<FieldType>
      label="Password"
      name="password"
      rules={[{ required: true, message: 'Please input your password!' }]}
    >
      <Input.Password />
    </Form.Item>

    <Form.Item<FieldType>
      name="remember"
      valuePropName="checked"
      wrapperCol={{ offset: 8, span: 16 }}
    >
      <Checkbox>Remember me</Checkbox>
    </Form.Item>

    <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
      <Button type="primary" htmlType="submit">
        Submit
      </Button>
    </Form.Item>
  </Form>
)};

export default Login;